﻿@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService
@inject IEventService _eventService
@inject IJSRuntime JS

<CnGalWebSite.Shared.MasaComponent.PC.Entries.PlayedGames.EditGameRecordTip @ref="editGameRecordTip" OnChanged="OnChanged" />
<GameRecordViewTip @ref="gameRecordViewTip" IsCurrentUser="IsCurrentUser" OnEdit="OnEdit" OnChanged="OnChanged" />

<style>
    .m-rating .m-icon {
        padding: 0.1rem;
    }
</style>
<MDCard Class="mb-4">
    <div style="display: flex; justify-content: space-around; flex-wrap: wrap; " class="pt-4">

        <div class="ps-4 pe-4" style="@((_dataCacheService.IsApp?" max-width: 400px;":" max-width: 200px;")+" display: flex; width: 100%; align-items: center;")">
            <MTextField Placeholder="可以搜索哦~" TValue="string" Value="@_dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.SearchString" ValueChanged="SearchValueChanged" OnKeyDown="OnKeyDown" Class="pt-0"></MTextField>
        </div>
        <div class="ps-4 pe-4" style="@((_dataCacheService.IsApp?" max-width: 400px;":" max-width: 200px;")+" width: 100%;")">
            <CnGalWebSite.Components.Inputs.MasaSelecte Value="_dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.SortType" ValueChanged="SortValueChanged" TValue="PlayedGamesSortType" Class="pt-0" />
        </div>
        @if(IsAnniversary)
        {
             <div class="ps-4 pe-4" style="@((_dataCacheService.IsApp?" max-width: 400px;":" max-width: 200px;")+" width: 100%;")">
            <CnGalWebSite.Components.Inputs.MasaSelecte Value="_dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.DisplayType" ValueChanged="DisplayValueChanged" TValue="PlayedGamesDisplayType" Class="pt-0" />
        </div>     
        }
        @*@if (IsAnniversary)
        {
            <div class="ps-4 pe-4" style=" display: flex; align-items: center;">
                <MButton Text Block Color="primary" OnClick="DomToImageLink">
                    <MIcon Left>mdi-share </MIcon>
                    分享我的游戏库
                </MButton>
            </div>
        }*@

    </div>
</MDCard>

<div class="row">
    @foreach (var item in Items.Skip((_dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.CurrentPage - 1) * _dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.MaxCount).Take(_dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.MaxCount))
    {
            <GameRecordViewCard Model="item" OnShow="OnShow" IsAnniversary="IsAnniversary" Type="_dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.DisplayType"/>
    }
</div>

@if (_dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.TotalPages > 1)
{
    <CnGalWebSite.Components.Paginations.MasaPagination Value="@_dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.CurrentPage" Length="@_dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.TotalPages" ValueChanged="OnPageClick" />
}
@if (Model.Any(s => s.IsHidden) && IsCurrentUser)
{

    <CnGalWebSite.Components.Containers.MasaTitleContainer Title="折叠的项目" Icon="fa fa-fw fa-star" @bind-IsCollapsed="IsCollapsed" Class="mb-3" Collapable>

        <div class="row">
            @foreach (var item in Model.Where(s => s.IsHidden))
            {
                <div class="col-6 col-md-6 col-lg-4 col-xl-3 mb-3">
                    <GameRecordViewCard Model="item" OnShow="OnShow" />
                </div>
            }
        </div>

    </CnGalWebSite.Components.Containers.MasaTitleContainer>
}


@code {
    [Parameter]
    public IEnumerable<GameRecordViewModel> Model { get; set; }

    [Parameter]
    public bool IsCurrentUser { get; set; }

    [Parameter]
    public bool IsAnniversary { get; set; }

    CnGalWebSite.Shared.MasaComponent.PC.Entries.PlayedGames.EditGameRecordTip editGameRecordTip;
    GameRecordViewTip gameRecordViewTip;
    IEnumerable<GameRecordViewModel> Items { get; set; } = Enumerable.Empty<GameRecordViewModel>();

    public bool IsCollapsed { get; set; } = true;

    protected override void OnInitialized()
    {

        Refresh();
    }

    public void OnChanged()
    {
        StateHasChanged();

    }

    public async Task OnShow(GameRecordViewModel model)
    {
        if (gameRecordViewTip != null)
        {
            gameRecordViewTip.Refresh(true, model);
        }
    }

    public async Task OnEdit(GameRecordViewModel model)
    {
        if (editGameRecordTip != null)
        {
            await editGameRecordTip.Refresh(true, model.GameId, model);
        }
    }

    public void Refresh()
    {
        Items = Model.Where(s => s.IsHidden == false && (s.ShowPublicly || IsCurrentUser));

        var searchString = _dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.SearchString;
        if (string.IsNullOrWhiteSpace(searchString))
        {
            Items = Items;
        }
        else
        {
            Items = Items.Where(s => (s.GameName?.Contains(searchString) ?? false) || (string.IsNullOrWhiteSpace(s.GameName) == false && (searchString?.Contains(s.GameName) ?? false)) ||
                      (s.GameBriefIntroduction?.Contains(searchString) ?? false) || (string.IsNullOrWhiteSpace(s.GameBriefIntroduction) == false && (searchString?.Contains(s.GameBriefIntroduction) ?? false)))
                       .ToList();
        }


        Items = _dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.SortType switch
        {
            PlayedGamesSortType.Score => Items.OrderByDescending(s => s.TotalSocre).ToList(),
            PlayedGamesSortType.Name => Items.OrderByDescending(s => s.GameName).ToList(),
            PlayedGamesSortType.Type => Items.OrderByDescending(s => s.Type).ToList(),
            PlayedGamesSortType.Time => Items.OrderByDescending(s => s.PlayDuration).ToList(),
            _ => Items
        };

        _dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.Count = Items.Count();
        _dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.CurrentPage = 1;
        StateHasChanged();
    }

    public void SortValueChanged(PlayedGamesSortType type)
    {
        _dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.SortType = type;
        _eventService.OnSavaTheme();
        Refresh();
    }
    public void DisplayValueChanged(PlayedGamesDisplayType type)
    {
        _dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.DisplayType = type;
        _eventService.OnSavaTheme();
        Refresh();
    }


    public void SearchValueChanged(string str)
    {
        _dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.SearchString = str;
        _eventService.OnSavaTheme();
        Refresh();
    }

    private void OnKeyDown(KeyboardEventArgs e)
    {
        if (e.Code == "Enter" || e.Code == "NumpadEnter")
        {
            Refresh();
        }
    }

    private void OnPageClick(int pageIndex)
    {
        _dataCacheService.ThemeSetting.AnniversariesSetting.PlayedGameSetting.CurrentPage = pageIndex;
        _eventService.OnSavaTheme();
        StateHasChanged();
    }

    private async Task DomToImageLink()
        {
            await JS.InvokeAsync<string>("domToImageUrl", "game-library-card","我的游戏库.jpg");
        }
}
